<template>
  <div>
    <div>
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <div class="users_main_head">
        <el-form-item  label="标签名称">
          <el-input
            v-model="form.tags"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
         <el-form-item label="城市">
          <el-select
            class="filter-item"
            v-model="form.province" placeholder="请选择"
            @keyup.enter="handleFilter"
            @change="handleProvince"
            filterable
          >
            <el-option v-for="item in citySelect.province" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item  >
        <el-form-item label="地区">
           <el-select
            class="filter-item"
             v-model="form.city" placeholder="请选择"
            @keyup.enter="handleFilter"
            filterable
          >
            <el-option v-for="item in citySelect.cityDate" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
       <el-form-item  label="企业简称">
          <el-input
            v-model="form.shortName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>

  <el-form-item label="状态" class="state">
    <el-select v-model="form.state" placeholder="请选择">
      <el-option label="启用" value="1"></el-option>
      <el-option label="禁止" value="0"></el-option>
    </el-select>
      </el-form-item>

        <el-form-item>
          <el-button @click="resetForm">清空</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
          <el-button
            type="success"
            class="newElement"
            @click="openDrawer('add')"
            >新增用户</el-button
          >
        </el-form-item>
      </div>
      <div class="uesrs_main_statistics">共{{total}}条记录</div>
      <el-table ref="table" :data="randerList" style="width: 100%">
        <el-table-column prop="id" label="序号" width="120"> </el-table-column>
        <el-table-column prop="number" label="企业编号" width="140">
        </el-table-column>
        <el-table-column prop="shortName" label="企业简称" width="110">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="170">
        </el-table-column>
        <el-table-column prop="creatorID" label="创建者" width="120">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="150">
        </el-table-column>
        <el-table-column prop="remarks" label="备注" width="135">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="150">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-row>
            <el-button @click="openDrawer('edit', row.id)" type="primary" icon="el-icon-edit" circle></el-button>
            <el-button v-if="row.state!=='启用'" @click="startUsing( row.id,row.state)" type="success" icon="el-icon-check" circle></el-button>
            <el-button v-else  @click="startUsing( row.id,row.state)"  icon="el-icon-close" circle></el-button>
             <el-button  @click="delDrawer(row.id)" type="danger" icon="el-icon-delete" circle></el-button>
             </el-row>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-form>
    <CompanysAdd @newDataes='newDataes' ref="popUp" :titleInfo='titleInfo' :formBase='formBase'></CompanysAdd>
  </div>
  </div>
</template>

<script>
import moment from 'moment'
import { provinces, citys } from '@/api/hmmm/citys.js'
import CompanysAdd from '@/module-hmmm/components/companys-add'
import { list, disabled, remove } from '@/api/hmmm/companys.js'
export default {
  components: {
    CompanysAdd
  },
  data () {
    return {
      citySelect: {
        province: [],
        cityDate: []
      },
      succeed: '已成功启用, 是否继续?',
      total: 0,
      query: {
        page: 1,
        pagesize: 10
      },
      form: {
        // 标签名称
        tags: '',
        // 城市
        province: '',
        // 地区
        city: '',
        // 企业简称
        shortName: '',
        // 状态
        state: null
      },
      randerList: [],
      formBase: {
        // 企业名称
        shortName: '',
        // 是否为名企
        isFamous: false,
        // 所属公司
        company: '',
        // 城市
        province: '',
        // 地区
        city: '',
        // 企业标签
        tags: '',
        // 备注
        remarks: '',
        id: 0,
        number: ''
      },
      titleInfo: {
        text: ''
      },
      type: ''
    }
  },

  created () {
    this.initData()
    this.getCityData()
  },

  methods: {
    async initData () {
      const { data } = await list(this.query)
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
        if (item.state === 1) {
          item.state = '启用'
        } else {
          item.state = '禁用'
        }
      })
    },
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
    },
    // 点击搜索发生什么
    async onSubmit () {
      const { data } = await list(this.form)
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
        item.state = item.state ? '启用' : '禁用'
      })
    },
    // 点击清空触发事件
    resetForm () {
      for (const key in this.form) {
        this.form[key] = ''
      }
      this.initData()
    },
    // 点击添加或修改
    openDrawer (type, id) {
      this.type = type
      this.$refs.popUp.dialogFormV()
      if (type === 'edit') {
        this.titleInfo.text = '编辑用户'
        this.randerList.forEach(item => {
          if (item.id === id) {
            this.formBase.shortName = item.shortName
            this.formBase.company = item.company
            this.formBase.province = item.province
            this.formBase.city = item.city
            this.formBase.remarks = item.remarks
            this.formBase.tags = item.tags
            this.formBase.id = id
            this.formBase.number = item.number
          }
        })
      } else {
        this.titleInfo.text = '创建用户'
      }
    },
    // 点击按钮启用或禁止
    async startUsing (id, state) {
      console.log(state)
      const obj = { id, state }
      if (obj.state === '启用') {
        obj.state = 0
      } else {
        obj.state = 1
      }
      await disabled(obj)
      this.initData()
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 点击删除
    async delDrawer (id) {
      this.$confirm('此操作将永久删除用户 , 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 提交表单
    newDataes () {
      this.initData()
      // this.$refs.popUp.handleResetForm()
      for (const key in this.formBase) {
        if (key === isFinite) {
          this.formBase[key] = false
        } else {
          this.formBase[key] = ''
        }
      }
    },
    // 分页
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.initData()
    }
  }
}
</script>

<style scoped lang='less'>

// 表单
.demo-form-inline {
  width: 1300px;
  height: 585px;
  padding: 0 20px;
  background-color: #fff;
  margin: 20px;
  .users_main_head {
    width: 100%;
    padding: 20px 0;
  }
}
/deep/ .el-form-item {
  margin-left: 37px;
}
.state{
  margin-left: 65px;
}
.newElement{
  margin-left: 630px;
}
.el-icon-edit {
  width: 35px;
  height: 35px;
  padding: 10px;
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  border-radius: 50%;
}
.el-icon-delete {
  width: 35px;
  height: 35px;
  padding: 10px;
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  border-radius: 50%;
  margin-left: 10px;
}
.el-icon-check{
  width: 35px;
  height: 35px;
  padding: 10px;
  color: #caeabe;
  background: #f0f9eb;
  border-color: #caeabe;
  border-radius: 50%;
  margin-left: 10px;
}
.el-icon-close{
  width: 35px;
  height: 35px;
  padding: 10px;
  color: #e7a258;
  background: #fdf6ec;
  border-color: #e7a258;
  border-radius: 50%;
  margin-left: 10px;
}
.uesrs_main_statistics {
  font-size: 13px;
  background-color: #f4f4f5;
  color: #909399;
  padding: 10px 20px;
}
</style>
